<?php

	// +----------------------------------------------------------------------
	// | 文档来源管理控制器
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\controller\cms\admin;

	use app\model\cms\Source as SourceModel;
	use yicmf\builder\Edit;
	use yicmf\builder\Table;
	use think\facade\Url;
	use app\controller\admin\Common;

	class Source extends Common
	{

		protected function initialize()
		{
			parent::initialize();
		}

		public function _filter()
		{
			return [];
		}

		public function index()
		{
			// 显示页面
			$builder = new Table();
			$builder->setSearchPostUrl('', '');
			$builder->model(SourceModel::class)
				->where($this->_filter())->suggest('在删除之后，前台显示默认本站链接，禁用则仅仅为发布文档的时候无法选择')
				->searchText('name', '标题', '标题')
				->buttonUpdate(url('update'))
				->keyId('id', '编号', 'desc')
				->keyText('name', '标题')
				->keyText('url', '地址')
				->keyText('use_time', '使用次数')
				->keyUser('user_id', '发布者')
				->keyText('remark', '备注')
				->keyText('update_time', '更新时间')
				->keyStatus()
				->data($data)
				->actionUpdate('update?id={$id}')
				->actionRestore('restore?id={$id}')
				->actionDelete('del?id={$id}')
				->actionForbid('forbid?id={$id}')
				->actionToCheck('forbid?id={$id}');

			return $builder->fetch();
		}

		public function update()
		{
			if ($this->request->isPost()) {
				parent::_update();
			} else {
				// 判断是否为编辑模式
				$isEdit = $this->request->has('id', 'get') ? true : false;
				$data = [];
				if ($isEdit) {
					$data = $this->model->get($this->request->get('id'));
				} else {
					$data['status'] = 1;
				}
				$builder = new Edit();
				$builder->title($isEdit ? '编辑来源' : '添加来源')
					->keyText('name', '标题', '文档页面显示的名字')
					->keyUrl('url', '链接地址', '完整的链接地址，需要以http(s)开头')
					->keyText('remark', '备注', '备注')
					->data($data)
					->buttonSubmit('', '保存')->buttonClose();

				return $builder->fetch();
			}
		}
	}
